var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var s = require('../../../utils/sign.js')
Page({

    data: {
        userinfo:[],
        setphone:[],
        img_url:c.img_url
    },

    onLoad: function (options) {
        this.setData({
            userinfo:wx.getStorageSync("userinfo")
        })
    },
    onShow:function(){
        this.setData({
            userinfo: wx.getStorageSync("userinfo")
        })
    },
    updata: function () {
      var that = this;
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          var data = {
            url: '/comv1/attachment/upimg',
            partner: 5500,
          };
          data['sign'] = s.sign(data);
          wx.uploadFile({
            url: c.api_url,
            filePath: tempFilePaths[0],
            name: 'file',
            formData: data,
            success: function (res) {
              //上传头像
              var data = JSON.parse(res.data);
              var userinfo = wx.getStorageSync("userinfo");
                  userinfo['pic'] = data.data.url;
              wx.setStorageSync('userinfo', userinfo);
              if (data.code == 0) {
                that.setData({
                  userinfo: userinfo
                });
              } else {
                utils.errMsg(data.msg);
              }

              var token = wx.getStorageSync("userinfo").wap_token;
              var data = {
                url: "/v1/user/edit",
                token: token,
                ext:'pic',
                value: data.data.url
              };
              utils.request(data).then(function (res) {
                if (res.code == 0) {
                  utils.sucMsg("保存成功");
                } else {
                  utils.errMsg(res.msg);
                }
              });

            }
          })
        }
      })
    },

})
